﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.Common.EventHandling;
using ScrumTable.Config.ElementManagement;
using ScrumTable.UI.ViewModel.Data;
using ScrumTable.UI.ViewModel.Dynamic;

#endregion

namespace ScrumTable.UI.ViewModel.Data
{
    public class VelocityIterationData : NotifyPropertyChanged
    {

        #region Decleration

        //--------------------------------------------------------------------
        // Decleration
        //--------------------------------------------------------------------

        private double _percent;
        private double _percentOpen;
        private double _percentEmpty;
        private double _points;
        private double _pointOpen;
        private bool _isSelected;
        private string _name;

        #endregion


        #region Properties

        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------


        public double Percent
        {
            get { return _percent; }
            set
            {
                _percent = value;
                SendPropertyChanged("Percent");
            }
        }

        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                _isSelected = value;
                SendPropertyChanged("IsSelected");
            }
        }


        public double PercentOpen
        {
            get { return _percentOpen; }
            set
            {
                _percentOpen = value;
                SendPropertyChanged("PercentOpen");
            }
        }

        public double PercentEmpty
        {
            get { return _percentEmpty; }
            set
            {
                _percentEmpty = value;
                SendPropertyChanged("PercentEmpty");
            }
        }

        public double Points
        {
            get { return _points; }
            set
            {
                _points = value;
                SendPropertyChanged("Points");
            }
        }

        public double PointOpen
        {
            get { return _pointOpen; }
            set
            {
                _pointOpen = value;
                SendPropertyChanged("PointOpen");
            }
        }

        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                SendPropertyChanged("Name");
            }
        }

        public Iteration Iteration { get; set; }

        #endregion


        #region Methods

        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------


        /// <summary>
        /// does only work if both have the same "name"
        /// </summary>
        /// <param name="data"></param>
        public void SetValues(VelocityIterationData data)
        {
            if (data != null && data.Name == Name)
            {
                PointOpen = data.PointOpen;
                Points = data.Points;
                PercentEmpty = data.PercentEmpty;
                PercentOpen = data.PercentOpen;
                Percent = data.Percent;
            }
        }

        /// <summary>
        /// Creates based on the project data a set of VelocityIterationData which the current velocity represents. 
        /// </summary>
        /// <param name="elementManager"></param>
        /// <param name="scrumData"></param>
        /// <param name="selectedIteration">Selects the iteration in the data.</param>
        /// <returns></returns>
        public static List<VelocityIterationData> CreateVelocityDataForProject(ElementManager elementManager, ScrumViewModelData scrumData, Iteration selectedIteration = null)
        {

            var velocity = from x in scrumData.CurrentProject.Iterations
                           select new VelocityIterationData()
                                      {
                                          Name = x.Name,
                                          Points = x.Stories.Where(y => elementManager.IsClosedState(y.State)).Sum(y => y.StoryPoints).GetValueOrDefault(0),
                                          PointOpen = x.Stories.Where(y => !elementManager.IsClosedState(y.State)).Sum(y => y.StoryPoints).GetValueOrDefault(0),
                                          Percent = 0,
                                          PercentOpen = 0,
                                          PercentEmpty = 0,
                                          Iteration = x,
                                          IsSelected = selectedIteration == x
                                      };

            var list = velocity.ToList();
            double max = Math.Max(1, list.Max(x => x.Points + x.PointOpen));

            const int maxLength = 100;
            foreach (var x in list)
            {
                x.Percent = (maxLength*x.Points/max).MinMax(0, 100); //at least 10% max 90%
                x.PercentOpen = (maxLength*x.PointOpen/max).MinMax(0, 100); //at least 10% max 90%
                //if percent is not 0 => min 10%
                if (x.Percent > 0 && x.Percent < 10)
                {
                    x.Percent = 10;
                }
                if (x.PercentOpen > 0 && x.PercentOpen < 10)
                {
                    x.PercentOpen = 10;
                }
                x.PercentEmpty = maxLength - (x.PercentOpen + x.Percent);
            }

            return list;

        }
    }
    #endregion

}